﻿using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;
using DataCenter.Configuration;
using Common.Contract.Customer.Identifier;

namespace DataCenter.Provider
{
    public class ShopBilltoProvider : GenericProvider, IShopBilltoProvider
    {
        public List<ShopBillto> Search(out int totalRecord, string shopCode = null, string shopName = null, string billToCode = null, string billToName = null, bool? transferFlag = null, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ShopBillto>();
            if (!string.IsNullOrWhiteSpace(shopCode))
            {
                querry = querry.Where(x => x.ShopCode.ToUpper() == shopCode.ToUpper());
            }

            //if (!string.IsNullOrWhiteSpace(shopName))
            //{
            //    querry = querry.Where(x => x.ShopName.ToUpper().Contains(shopName.ToUpper()));
            //}

            if (!string.IsNullOrWhiteSpace(billToCode))
            {
                querry = querry.Where(x => x.BilltoCode.ToUpper().Contains(billToCode.ToUpper()));
            }

            //if (!string.IsNullOrWhiteSpace(billToName))
            //{
            //    querry = querry.Where(x => x.BilltoN.ToUpper() == billToCode.ToUpper());
            //} 

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (!transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }

            querry = querry.OrderBy(x => x.ShopCode);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            var result = querry.ToList();
            return result;
        }

        public ShopBillto Retrieve(string shopcode, string billtoCode)
        {
            shopcode = (shopcode ?? string.Empty).ToUpper();
            billtoCode = (billtoCode ?? string.Empty).ToUpper();
            return repository.FindOne<ShopBillto>(x => x.ShopCode.ToUpper() == shopcode && x.BilltoCode.ToUpper() == billtoCode);
        }

        public bool Update(ShopBillto shop)
        {
            try
            {
                var shopChecked = Retrieve(shop.ShopCode, shop.BilltoCode);
                if (shopChecked == null) return false;
                if (shop.SoTransferDate.HasValue) shopChecked.SoTransferDate = shop.SoTransferDate;
                if (!shop.SoTransferFlag.IsNullOrWhiteSpace()) shopChecked.SoTransferFlag = shop.SoTransferFlag;
                if (!shop.UseFlag.IsNullOrWhiteSpace()) shopChecked.UseFlag = shop.UseFlag;
                if (!shop.ModifyUser.IsNullOrWhiteSpace()) shopChecked.ModifyUser = shop.ModifyUser;
                if (shop.CreateDate.HasValue) shopChecked.CreateDate = shop.CreateDate;
                if (!shop.CreateUser.IsNullOrWhiteSpace()) shopChecked.CreateUser = shop.CreateUser;
                if (shop.ModifyDate.HasValue) shopChecked.ModifyDate = shop.ModifyDate;

                shopChecked.ShopCode = shop.ShopCode;
                repository.UnitOfWork.SaveChanges();
                return true;

            }
            catch
            {
                return false;
            }
        }

        public bool Add(ShopBillto shop)
        {
            try
            {
                repository.ShopBillto.Add(shop);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string shopcode, string billtoCode)
        {
            try
            {
                shopcode = (shopcode ?? string.Empty).ToUpper();
                billtoCode = (billtoCode ?? string.Empty).ToUpper();
                repository.Delete<ShopBillto>(x => x.ShopCode.ToUpper() == shopcode && x.BilltoCode.ToUpper() == billtoCode);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

